bg='#282828'

# ==========================
# ===  General settings  ===
# ==========================

# https://gist.github.com/bbqtd/a4ac060d6f6b9ea6fe3aabe735aa9d95
set -g default-terminal "tmux-256color"

set -g history-limit 10000

set -s focus-events on

set -g mouse on

# Index starts from 1
set -g base-index 1
setw -g pane-base-index 1   # make pane numbering consistent with windows

setw -g automatic-rename on # rename window to reflect current program
set -g renumber-windows on  # renumber windows when a window is closed

set -g set-titles on                        # set terminal title
set -g set-titles-string 'tmux-#S #I:#W'

# reduce the time when type prefix
set -s escape-time 0

setw -g monitor-activity on
set -g visual-activity off

set -g repeat-time 400
# ==========================
# ===    Colorscheme     ===
# =========================,bg==
set -g message-style fg='colour222',bg='colour238'
set -g message-command-style fg='colour222',bg='colour238'

split_win='#504945'
set-option -g status-style                 fg=colour15,bg=$bg
set-option -g pane-border-style            fg=$split_win,bg=$bg
set-option -g pane-active-border-style     fg=green,bg=$bg
set-option -g window-status-activity-style fg=colour111

# =====================================
# ===    Appearence and status bar  ===
# =====================================
set -g status-interval 2
set-option -g status-fg default

set -g status on
set -g status-justify left
set -g status-position bottom

set -g status-left-length 80
set -g status-right-length 80

set-option -g status-left ""
# prefix
prefix="#{?client_prefix,⚡,}"

# Window status
inactive='#222829'
grey='#74787a'
yellow='#f2c867'
teal='#37ad88'
green='#afd787'
purple='#d3869b'

set-window-option -g window-status-separator ''
set -g status-left " "
set -g status-right "$prefix #[bg=$bg,fg=blue]  #[fg=blue,bg=$bg]#{s|$HOME|~|:pane_current_path} #[fg=$purple,bg=$bg] #[fg=$purple,bg=$bg]#S "
set-option -g window-status-format "#[bg=$bg,fg=$grey] #[fg=$grey,bg=$bg]#W  "
set-option -g window-status-current-format "#[fg=$yellow,bg=$bg] #[fg=$green,bg=$bg]#W  "

# ==========================
# ===   Key bindings     ===
# ==========================
setw -g xterm-keys on
# use vi mode
set -g mode-keys vi
# set prefix key
set -g prefix C-Space
# unbind the default prefix key C-b
unbind C-b
unbind '"'
unbind %
bind h split-window -hb -c "#{pane_current_path}"
bind j split-window -v -c "#{pane_current_path}"
bind k split-window -vb -c "#{pane_current_path}"
bind l split-window -h -c "#{pane_current_path}"

# switch window
bind -n M-h select-pane -L
bind -n M-j select-pane -D
bind -n M-k select-pane -U
bind -n M-l select-pane -R

# change the window size
bind -n M-H resize-pane -L 10
bind -n M-J resize-pane -D 10
bind -n M-K resize-pane -U 10
bind -n M-L resize-pane -R 10

# create new window with new name
# bind n new-window -c "#{pane_current_path}"
bind n new-window -c "$HOME"
bind N command-prompt -p "window name:" "new-window; rename-window '%%'"
bind Q kill-pane
bind o break-pane
bind -n M-p previous-window
bind -n M-n next-window

# copy mode
bind -n M-v copy-mode
bind -T copy-mode-vi v send-keys -X begin-selection

# reload the tmux config
bind r source-file ~/.tmux.conf \; display "Configuration Reloaded"

# Toggle popup window with Alt-f
# bind-key -n -N 'Toggle popup window' M-f if-shell -F '#{==:#{session_name},popup}' {
#     detach-client
# } {
#     display-popup -d "#{pane_current_path}" -x C -y C -w 80% -h 75% -E 'tmux attach-session -t popup || tmux new-session -s popup'
# }

bind-key -n M-q display-menu -x C -y C \
    "New Session"                        S "command-prompt -p \"New Session:\" \"new-session -A -s '%%'\"" \
    "Kill Session"                       x "kill-session" \
    "Kill Other Session(s)"              X "kill-session -a" \
    "" \
    "New Window"                         c new-window \
    "Kill Window"                        k killw  \
    "Choose Window"                      w choose-window \
    "Previous Window"                    p previous-window \
    "Next Window"                        n next-window \
    "Swap Window Right"                  ↑ "swap-window -t -1" \
    "Swap Window Left"                   ↓ "swap-window -t +1" \
    "Horizontal Split"                   i "split-window -h" \
    "Vertical Split"                     e "split-window -v"  \
    "" \
    "Swap Pane Up"                       < "swap-pane -U" \
    "Swap Pane Down"                     > "swap-pane -D" \
    "Break Pane"                         t break-pane \
    "Join Pane"                          j "choose-window 'join-pane -h -s \"%%\"'" \
    "#{?window_zoomed_flag,Unzoom,Zoom}" z "resize-pane -Z"\
    "Close Menu"                         q ""

# ==========================
# ===   Plugin Resurrect ===
# ==========================

# tmux-resurrect
set -g @resurrect-capture-pane-contents 'on'
# save vim session
set -g @resurrect-strategy-vim 'session'
# save neovim session
set -g @resurrect-strategy-nvim 'session'

# ============================
# ===       Plugins        ===
# ============================
set-environment -g TMUX_PLUGIN_MANAGER_PATH '~/.local/share/tmux/plugins'
set -g @plugin 'tmux-plugins/tpm'
set -g @plugin 'tmux-plugins/tmux-resurrect'
set -g @plugin 'tmux-plugins/tmux-continuum'
set -g @plugin 'tmux-plugins/tmux-prefix-highlight'
set -g @plugin 'tmux-plugins/tmux-copycat'
set -g @plugin 'tmux-plugins/tmux-yank'
set -g @prefix_highlight_show_copy_mode 'on'
set -g @prefix_highlight_copy_mode_attr 'fg=white,bg=blue'
set -g @continuum-save-interval '15'

if "test ! -d ~/.local/share/tmux/plugins/tpm" \
   "run 'git clone https://github.com/tmux-plugins/tpm ~/.local/share/tmux/plugins/tpm && ~/.local/share/tmux/plugins/tpm/bin/install_plugins'"

run '~/.local/share/tmux/plugins/tpm/tpm'
